/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.servlet;

import com.blandware.atleap.webapp.util.core.StatisticsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>Servlet that collects statistics.</p>
 * <p><a href="StatisticsCollectionServlet.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2008/01/30 08:57:23 $
 * @web.servlet name="statisticsCollection" load-on-startup="1"
 * @web.servlet-init-param name="isEnabled" value="${statistics.enabled}"
 * @web.servlet-mapping url-pattern="/servlet/collectStats/*"
 */
public class StatisticsCollectionServlet extends HttpServlet {

	/**
	 * Commons Logging instance.
	 */
	protected transient final Log log = LogFactory.getLog(StatisticsCollectionServlet.class);

    /**
     * Whether statistics collection is enabled
     */
    protected boolean isEnabled = true;

    /**
	 * Initializes the servlet.
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
        isEnabled = Boolean.valueOf(getInitParameter("isEnabled")).booleanValue();
    }

	/**
	 * Destroys the servlet.
	 */
	public void destroy() {
        super.destroy();
	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
	 *
	 * @param request  servlet request
	 * @param response servlet response
     * @throws java.io.IOException  if i/o error occures
     * @throws javax.servlet.ServletException   if servler error occures
	 */
	protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response)
	        throws ServletException, IOException {
        if (isEnabled) {
            StatisticsUtil.serveVisit(request, response, true);
        }

        //no-cache actions
		response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
		response.setHeader("Pragma", "no-cache");
    }

    /**
	 * Handles the HTTP <code>GET</code> method.
	 *
	 * @param request  servlet request
	 * @param response servlet response
	 */
	protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
	        throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 *
	 * @param request  servlet request
	 * @param response servlet response
	 */
	protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
	        throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 */
	public String getServletInfo() {
		return "Servlet to collect statistics data";
	}
}
